package com.dswz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dswz.dao.UserDao;
import com.dswz.po.User;
import com.dswz.po.UserQuery;
import com.dswz.service.UserService;
import com.dswz.utils.DateUtil;
import com.dswz.utils.HttpClientUtil;
import com.dswz.utils.PageBean;
import com.dswz.vo.VoUser;
import com.google.gson.JsonParser;

@Service("userService")

@Transactional
public class UserServiceImpl implements UserService {
	private Logger logger = LogManager.getLogger(UserServiceImpl.class);
	@Autowired
	private UserDao userDao;

	@Transactional(readOnly = true)
	public PageBean getUserList(String username, String userMobile, String userName, String userCreateTime, Byte isDel,
			Integer page, Integer size) throws Exception {
		int count = 0;
		List<User> userList = new ArrayList();
		UserQuery userQuery = new UserQuery();

		userQuery.setPageNo(page);

		userQuery.setPageSize(size);

		userQuery.setOrderByClause("user_order_no desc,user_create_time desc");
		Date endP;
		if (username.equals("admin")) {
			UserQuery.Criteria createCriteria = userQuery.createCriteria();

			if (StringUtils.isNotEmpty(userMobile)) {
				createCriteria.andUserMobileEqualTo(userMobile);
			}
			if (StringUtils.isNotEmpty(userName)) {
				createCriteria.andUserNameEqualTo(userName);
			}
			if (StringUtils.isNotEmpty(userCreateTime)) {
				String[] split = userCreateTime.split(",");
				if (split.length == 2) {
					Date startP = DateUtil.parseDate(split[0]);

					endP = DateUtil.addDate(DateUtil.parseSqlDate(split[1]), 1);
					createCriteria.andUserCreateTimeBetween(startP, endP);
				} else {
					Date startP = DateUtil.parseDate(split[0]);
					createCriteria.andUserCreateTimeGreaterThanOrEqualTo(startP);
				}
			}

			if (isDel != null) {
				createCriteria.andUserIsDelEqualTo(isDel);
			}
			count = this.userDao.countByExample(userQuery);
			userList = this.userDao.selectByExample(userQuery);
		} else {
			UserQuery.Criteria createCriteria = userQuery.createCriteria();

			if (isDel != null) {
				createCriteria.andUserIsDelEqualTo(isDel);
			}
			count = this.userDao.countByExample(userQuery);
			userList = this.userDao.selectByExample(userQuery);
		}
		PageBean pb = new PageBean(size.intValue(), count, page.intValue() + 1);

		List<VoUser> voUserList = new ArrayList();
		if ((userList != null) && (userList.size() > 0)) {
			for (User user : userList) {
				VoUser voUser = new VoUser();
				BeanUtils.copyProperties(user, voUser);
				voUserList.add(voUser);
			}
		}
		pb.setContentList(voUserList);
		return pb;
	}

	public void insertUser(User user) throws Exception {
		user.setUserAuthStatus(Byte.valueOf((byte) 0));
		user.setUserIsFreeze(Byte.valueOf((byte) 0));

		if (user.getUserOrderNo() == null) {
			user.setUserOrderNo(Integer.valueOf(0));
		}

		user.setUserIsDel(Byte.valueOf((byte) 0));

		user.setUserCreateTime(new Date());
		this.userDao.insertSelective(user);
	}

	public void deleteUsers(List<String> userIds) throws Exception {
		User user = new User();
		user.setUserIsDel(Byte.valueOf((byte) 1));
		if ((userIds != null) && (userIds.size() > 0)) {
			for (String userId : userIds) {
				user.setUserId(userId);
				this.userDao.updateByPrimaryKeySelective(user);
			}
		}
	}

	public void deleteUser(String userId) throws Exception {
		User user = new User();
		user.setUserIsDel(Byte.valueOf((byte) 1));
		user.setUserId(userId);
		this.userDao.updateByPrimaryKeySelective(user);
	}

	public void updateUser(User user) throws Exception {
		this.userDao.updateByPrimaryKeySelective(user);
	}

	@Transactional(readOnly = true)
	public User getUserById(String id) throws Exception {
		return this.userDao.selectByPrimaryKey(id);
	}

	public VoUser editUser(String userId) throws Exception {
		User user = this.userDao.selectByPrimaryKey(userId);
		VoUser voUser = new VoUser();
		BeanUtils.copyProperties(user, voUser);
		return voUser;
	}

	@Transactional(readOnly = true)
	public User getUser(String userMobile, Byte isDel) throws Exception {
		UserQuery userQuery = new UserQuery();

		UserQuery.Criteria createCriteria = userQuery.createCriteria();
		createCriteria.andUserMobileEqualTo(userMobile);

		if (isDel != null) {
			createCriteria.andUserIsDelEqualTo(isDel);
		}
		List<User> userList = this.userDao.selectByExample(userQuery);
		if ((userList != null) && (userList.size() > 0)) {
			return (User) userList.get(0);
		}
		return null;
	}

	public void sendSmsValidateCode(String mobile, String code, String template) {
		String result2 = "";
		Map params = new HashMap();
		params.put("ac", "send");
		params.put("uid", "shanxinw");
		params.put("pwd", "08313319674596f388f63bd5604875cb");
		params.put("template", template);
		params.put("mobile", mobile);
		params.put("content", code);
		try {
			String result = HttpClientUtil.doGet2("http://api.sms.cn/sms/", params);
			result2 = new JsonParser().parse(result).getAsJsonObject().get("message").getAsString();
			if ("发送成功".equals(result2)) {
				this.logger.info("短信发送成功:" + result);
			} else {
				this.logger.error("短信发送失败" + result);
			}
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
		}
	}
}

/*
 * Location:
 * D:\soft\jd-gui-windows-1.4.0\xiaokangajjia.zip!\WEB-INF\classes\com\dswz\
 * service\impl\UserServiceImpl.class Java compiler version: 7 (51.0) JD-Core
 * Version: 0.7.1
 */